<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;

class AboutCompanyController extends Controller
{
    public function aboutCompany(){

        $result = DB::table('aboutcompany')->where('aboutcompany_id','=','7')->get();

        return view('Admin.aboutcompany',['result' => $result]);
    }

    public function CompanyImgUpload(Request $request)
    {



        $movieArray = array('mp4','mkv','3gp','avi','mov','wmv','flv','rmvb');
        $imageArray = array('jpg','bmp','gif','jpeg','psd','png');

        $allPath = array();
        if($request->isMethod('post')){
            $data = $request->all();
            if(isset($data['aboutcompany_image'])&&!empty($data['aboutcompany_image'])){
                foreach ($data['aboutcompany_image'] as $oneData){
//                dd($oneData);
                    $path = $this->doUpload($oneData);
                    array_push($allPath,$path);
                }

            }
            $MStr = null;
            $ImageStr = null;

            foreach ($allPath as $pathData){
                if(in_array($pathData[0],$imageArray)){
                    $ImageStr .= $pathData['path'].'+';
                }

                if(in_array($pathData[0],$movieArray)){
                    $MStr .= $pathData['path'].'+';
                }

            }

           // dd($ImageStr);





//            dd($movieData);

            $test = DB::table('aboutcompany')->where('aboutcompany_id','=','7')->update(['aboutcompany_image'=>$ImageStr]);

            if($test){
                return redirect('/admin/aboutCompany');
            }else{
                return json_encode(['code'=>'0','msg'=>'faied']);
            }
        }
    }

    public function doUpload($file)
    {
        $movieArray = array('mp4', 'mkv', '3gp', 'avi', 'mov', 'wmv', 'flv', 'rmvb');
        if ($file->isValid()) {
            // 获取文件相关信息
            $originalName = $file->getClientOriginalName(); // 文件原名
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            $type = $file->getClientMimeType();     // image/jpeg
            // 上传文件
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            // 使用我们新建的uploads本地存储空间（目录）
            $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
            if (true == $bool) {
                $SQLFileName['path'] = 'uploads/' . $filename;
                array_push($SQLFileName, $ext);


                return $SQLFileName;


            }
        }
    }


        public function CompanyImgUpload1(Request $request){

        if($request->isMethod('POST')){

            $file = $request->file('aboutcompany_image');
            var_dump($file);
            if($file->isValid()){

                $mimetype = $file->getClientMimeType();
                $originalName = $file->getClientOriginalName();

                $ext = $file->getClientOriginalExtension();
                $realPath = $file->getRealPath();

                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;

                $bool = Storage::disk('uploads')->put($filename,file_get_contents($realPath));



                $path = "/../public/uploads/";
                $url = '/uploads/'.$filename;

                $result = DB::table('aboutcompany')->where('aboutcompany_id','=','7')->update([
                    'aboutcompany_image'=> $url,
                ]);
                if($result){
                    return redirect('/admin/aboutCompany');
                }
            }
            exit;
        }

    }
    public function updateAboutCompany(Request $request){


        try{
            $input['editorValue'] = $request->input('editorValue');

            $validator = Validator::make($input, [
                'editorValue'=> 'required'
            ]);

            if ($validator->fails()) {
                return $validator->messages();
            }

            $data['aboutcompany_content'] = $input['editorValue'];

            $result = DB::table('aboutcompany')->where('aboutcompany_id','=','7')->update($data);
            if($result){

                return redirect('/admin/aboutCompany');
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

}
